package com.wootide.cms.common.manager;

import java.io.Serializable;
import java.util.List;

public class PageEntity implements Serializable
{
    /**
     * 意义，目的和功能，以及被用到的地方<br>
     */
    private static final long serialVersionUID = 1L;
    
    /**Record number*/
    private int totalSize = 0;
    
    /**the total number of pages*/
    private int totalPage = 0;
    
    /**The current page*/
    private int pageIndex = 0;
    
    /**the record number of every page shows */
    private int pageSize = 2;
    
    @SuppressWarnings("unchecked")
    private List pageList = null;
    
    /** ADD BY GL ***/
    private int pageListSize = 0;
    
    private int startPage;
    
    private int endPage;
    
    private int begin;
    
    private int nextPage;
    
    private int prePage;
    
    private String nextPageUrl;
    
    private String prePageUrl;
    
    private String toPage;
    
    private String sql;
    
    public String pageBottom;
    
    public PageEntity()
    {
        
    }
    
    /**
     * 
     * @param resultList 
     * @param totalCou 
     * @param pageSize 
     * @param startPage 
     * @param url 
     */
    @SuppressWarnings("unchecked")
    public PageEntity(List resultList, int totalCou, int pageSize, int startPage)
    {
        this.pageListSize = (resultList == null || resultList.size() == 0) ? 0
                : resultList.size();
        this.startPage = startPage;
        this.totalSize = totalCou;
        this.pageSize = pageSize;
        String pre = "?";
        if (resultList != null)
        {
            int maxIndex = Math.max(this.totalSize / this.pageSize
                    + ((this.totalSize % this.pageSize) > 0 ? 1 : 0), 1);
            
            this.pageList = resultList;
            this.pageIndex = startPage;
            this.totalPage = maxIndex;
            this.nextPage = getNextPage(this.pageIndex, this.totalPage);
            this.prePage = getPrePage(this.pageIndex);
            
            if (this.pageIndex == 1)
            {
                
                this.prePageUrl = MessageUtil.getResource("page.entity.prePageUrl.index.y");
                
            }
            else
            {
                this.prePageUrl = MessageUtil.getResource("page_entity_prePageurl.index.n",
                        new Object[] {pre, this.prePage});
            }
            
            if (this.pageIndex == totalPage)
            {
                this.nextPageUrl = MessageUtil.getResource("page.entity.nextPageUrl.one");
            }
            else
            {
                this.nextPageUrl = MessageUtil.getResource("page.entity.nextPageUrl.two",
                        new Object[] {pre, this.nextPage});
            }
            
            this.pageBottom = MessageUtil.getResource("page.entity.pageBottom.one",
                    new Object[] {totalCou, totalPage})
                    + MessageUtil.getResource("page.entity.pageBottom.two",
                            new Object[] {pre})
                    + prePageUrl
                    + "&nbsp;&nbsp;"
                    + "&nbsp;&nbsp;"
                    + nextPageUrl
                    
                    + MessageUtil.getResource("page.entity.pageBottom.three",
                            new Object[] {pre, totalPage})
                    
                    + MessageUtil.getResource("page.entity.pageBottom.four",
                            new Object[] {pageIndex});
        }
    }
    
    /**
     * 
     * @param pageSize 
     * @param startPage 
     * @return
     */
    public int getBegin(int pageSize, int startPage)
    {
        this.begin = (startPage - 1) * pageSize + 1;
        return begin - 1;
    }
    
    /**
     * 
     * @param pageIndex 
     * @param totalPage 
     * @return
     */
    
    public int getNextPage(int pageIndex, int totalPage)
    {
        if (totalPage == 0)
        {
            return 1;
        }
        return pageIndex == totalPage ? totalPage : (pageIndex + 1);
        
    }
    
    /**
     * 
     * @param pageIndex 
     * @return
     */
    public int getPrePage(int pageIndex)
    {
        
        return pageIndex == 1 ? 1 : (pageIndex - 1);
        
    }
    
    /**
     * 
     * @param pageIndex 
     * @param pageSize 
     * @return
     */
    public static int getBeginIndex(int pin, int pagesize)
    {
        int begin = ((pin - 1)) * pagesize < 0 ? 0 : ((pin - 1) * pagesize);
        return begin;
    }
    
    public int getNextPage()
    {
        return nextPage;
    }
    
    public void setNextPage(int nextPage)
    {
        this.nextPage = nextPage;
    }
    
    public String getNextPageUrl()
    {
        return nextPageUrl;
    }
    
    public void setNextPageUrl(String nextPageUrl)
    {
        this.nextPageUrl = nextPageUrl;
    }
    
    public int getPrePage()
    {
        return prePage;
    }
    
    public void setPrePage(int prePage)
    {
        this.prePage = prePage;
    }
    
    public String getPrePageUrl()
    {
        return prePageUrl;
    }
    
    public void setPrePageUrl(String prePageUrl)
    {
        this.prePageUrl = prePageUrl;
    }
    
    public int getBegin()
    {
        return begin;
    }
    
    public void setBegin(int begin)
    {
        this.begin = begin;
    }
    
    public String getPageBottom()
    {
        return pageBottom;
    }
    
    public void setPageBottom(String pageBottom)
    {
        this.pageBottom = pageBottom;
    }
    
    public int getEndPage()
    {
        return endPage;
    }
    
    public void setEndPage(int endPage)
    {
        this.endPage = endPage;
    }
    
    public int getPageIndex()
    {
        return pageIndex;
    }
    
    public void setPageIndex(int pageIndex)
    {
        this.pageIndex = pageIndex;
    }
    
    @SuppressWarnings("unchecked")
    public List getPageList()
    {
        return pageList;
    }
    
    @SuppressWarnings("unchecked")
    public void setPageList(List pageList)
    {
        this.pageList = pageList;
    }
    
    public int getPageSize()
    {
        return pageSize;
    }
    
    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }
    
    public String getSql()
    {
        return sql;
    }
    
    public void setSql(String sql)
    {
        this.sql = sql;
    }
    
    public int getStartPage()
    {
        return startPage;
    }
    
    public void setStartPage(int startPage)
    {
        this.startPage = startPage;
    }
    
    public int getTotalPage()
    {
        return totalPage;
    }
    
    public void setTotalPage(int totalPage)
    {
        this.totalPage = totalPage;
    }
    
    public int getTotalSize()
    {
        return totalSize;
    }
    
    public void setTotalSize(int totalSize)
    {
        this.totalSize = totalSize;
    }
    
    public String getToPage()
    {
        return toPage;
    }
    
    public void setToPage(String toPage)
    {
        this.toPage = toPage;
    }
    
    public int getPageListSize()
    {
        return pageListSize;
    }
    
    public void setPageListSize(int pageListSize)
    {
        this.pageListSize = pageListSize;
    }
    
}
